(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.rptViewController', rptViewController);
	rptViewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ajaxServiceFactory'];
	function rptViewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("companyRptViewPopup"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getCompanyRptOptions : {
				url : 'do/cda/dataList/companyRptOptions',
				method : 'get'
			},
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.companyRpt = {};
		ctrl.companyRptOld = {};
		ctrl.formName = "companyRptForm";
		
		ctrl.init = function() {
			ctrl.reportParameterGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.reportParmsCfg.tableConfigs.table);
			ctrl.reportParameterGridOptions.data = 'vm.companyRpt.reportParms';
			ctrl.reportParameterGridOptions.verifyData  = 'vm.companyRptOld.reportParms';
			ctrl.availableFormatsOptions = getReportFormatsOptions();
			ctrl.reportCodeOptions = {
				"id" : "cda.report",
				"params" : {
					"filterReportCodes" : "<filterReportCodes"
				},
				"okEvent" : true,
				"cancelEvent" : false
			};
			//
			ajaxService.getCompanyRptOptions().then(function(response) {
				var companyRptOptions = {};
				if (response.data) {
					angular.forEach(response.data, function(value, key) {
						var tempValue = {};
						tempValue.data = value;
						companyRptOptions[key] = tempValue;
					});
				}
				ctrl.yesnoOptions = companyRptOptions.yesnoOptions;
				ctrl.archiveMethodOptions = companyRptOptions.archiveMethodOptions;
				ctrl.localeCodeOptions = companyRptOptions.localeCodeOptions;
				//
				ctrl.companyRpt = ctrl.dialogsModule.params.companyRpt || {};
				ctrl.companyRpt.reportParms = ctrl.companyRpt.reportParms || [];
				ctrl.companyRptOld = ctrl.dialogsModule.params.companyRptOld;
				if (ctrl.companyRptOld)
					ctrl.companyRptOld.reportParms = ctrl.companyRptOld.reportParms || [];
			});
		};
		
		function getReportFormatsOptions() {
			return {
				"data" : [ {
					"label" : "PDF",
					"value" : "PDF"
				},{
					"label" : "XLS",
					"value" : "XLS"
				},{
					"label" : "CSV",
					"value" : "CSV"
				},{
					"label" : "HTML",
					"value" : "HTML"
				}]
			};
		}
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
	}
})(angular);